package org.farng.mp3.lyrics3;

import org.farng.mp3.AbstractMP3Tag;
import org.farng.mp3.TagException;
import org.farng.mp3.TagNotFoundException;
import org.farng.mp3.id3.ID3v1;

import java.io.IOException;
import java.io.RandomAccessFile;


/**
 * Title:       Lyrics3 Description: This class represents a Lyrics3 1.00 tag
 * Copyright:   Copyright (c) 2002 Company:
 *
 * @author Eric Farng
 * @version $Revision: 1.5 $
 */
public abstract class AbstractLyrics3 extends AbstractMP3Tag {
    public AbstractLyrics3() {}

    public AbstractLyrics3(AbstractLyrics3 copyObject) {
        super(copyObject);
    }

    /**
     * DOCUMENT ME!
     *
     * @param file DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     * @throws TagException DOCUMENT ME!
     */
    public void append(RandomAccessFile file)
                throws IOException, TagException {
        AbstractLyrics3 oldTag;

        try {
            oldTag = new Lyrics3v2(file);
            oldTag.append(this);
            oldTag.write(file);
        } catch (TagNotFoundException ex) {
            try {
                oldTag = new Lyrics3v1(file);
                oldTag.append(this);
                oldTag.write(file);
            } catch (TagNotFoundException ex2) {
                this.write(file);
            }
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param file DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     */
    public void delete(RandomAccessFile file)
                throws IOException {
        long  filePointer;
        ID3v1 id3v1tag = new ID3v1();

        if (seek(file)) {
            id3v1tag = id3v1tag.getID3tag(file);

            seek(file);
            filePointer = file.getFilePointer();

            // cut off the "LYRICSBEGIN"
            filePointer -= 11;
            file.setLength(filePointer);
            file.seek(file.length());

            if (id3v1tag != null) {
                id3v1tag.write(file);
            }
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param file DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     * @throws TagException DOCUMENT ME!
     */
    public void overwrite(RandomAccessFile file)
                   throws IOException, TagException {
        AbstractLyrics3 oldTag;

        try {
            oldTag = new Lyrics3v2(file);
            oldTag.overwrite(this);
            oldTag.write(file);
        } catch (TagNotFoundException ex) {
            try {
                oldTag = new Lyrics3v1(file);
                oldTag.overwrite(this);
                oldTag.write(file);
            } catch (TagNotFoundException ex2) {
                this.write(file);
            }
        }
    }
}